from finetune.finetune import FineTune
import os
import jsonlines
from pathlib import Path




FT_EXP_BASE_PATH = "F:/vs_code/file/test-finetune/finetune-01/output2"
FT_OUT_PATH = "F:/vs_code/file/test-finetune/finetune-01/output2/synthesize/result"
ft_model = FineTune()

data_path = "F:/vs_code/file/test-finetune/finetune-01/input/yanjun_01"
data_base = data_path.split(os.sep)[-1]
exp_dir = FT_EXP_BASE_PATH

NOW_FILE_PATH = os.path.dirname(__file__)
MAIN_ROOT = os.path.realpath(os.path.join(NOW_FILE_PATH, "../../../../"))
print(NOW_FILE_PATH)
print(MAIN_ROOT)
print(data_base)
print(exp_dir)
print("======================================================================================")
print("================start=================================================================")
print("=================英文数据集：F:/vs_code/file/test-finetune/finetune-01/input/SSB0005_mini")
print("======================================================================================")

def finetune():

    pretrained_model_dir = os.path.realpath(
        "F:/vs_code/file/test-finetune/finetune-01/pretrained_models/fastspeech2_mix_ckpt_1.2.0")
    voc_model_dir = os.path.realpath(
        "F:/vs_code/file/test-finetune/finetune-01/pretrained_models/hifigan_aishell3_ckpt_0.2.0")
    
    exp_dir_end = ft_model.finetune(
            pretrained_model_dir=pretrained_model_dir,
            voc_model_dir=voc_model_dir,
            input_dir=os.path.realpath(data_path),
            exp_dir=os.path.realpath(exp_dir))
    if exp_dir_end:
        print("微调SUCCESS")
    else:
        print("微调失败")

def synthesize():
    if not os.path.exists(exp_dir):
        print("模型路径不存在")
        return
    wav_name = "aaaa01"
    text = "根据巴西巴拉那州民防部门当地时间11月4日下午发布的数据，受强降雨影响，该州处于紧急状态的城镇从3日的17个增至27个。在过去9天内，强降雨已波及该州131个城镇，超过9万人受到不同程度的影响。"

    pretrained_model_dir = os.path.realpath(
        "F:/vs_code/file/test-finetune/finetune-01/pretrained_models/fastspeech2_mix_ckpt_1.2.0")
    voc_model_dir = os.path.realpath(
        "F:/vs_code/file/test-finetune/finetune-01/pretrained_models/hifigan_aishell3_ckpt_0.2.0")
    
    wav_path = ft_model.synthesize(
        pretrained_model_dir=pretrained_model_dir,
        voc_model_dir=voc_model_dir,
        text= text,
        wav_name=wav_name,
        out_wav_dir=os.path.realpath(FT_OUT_PATH),
        exp_dir=os.path.realpath(exp_dir))
    if wav_path:
        res = {"wavName": wav_name + ".wav", "wavPath": wav_path}
        print(str("合成成功："+ res))
    else:
        print("音频合成失败")


#训练
finetune()

#合成
# synthesize()
